Conditions | 1 |
Paths | 256 |
Total Lines | 76 |
Code Lines | 52 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /** |
||
39 | $(function() { |
||
40 | /** |
||
41 | * Add the data-method="delete" forms to all delete links |
||
42 | */ |
||
43 | addDeleteForms() |
||
44 | |||
45 | /** |
||
46 | * Disable all submit buttons once clicked |
||
47 | */ |
||
48 | $('form').submit(function() { |
||
49 | $(this) |
||
50 | .find('input[type="submit"]') |
||
51 | .attr('disabled', true) |
||
52 | $(this) |
||
53 | .find('button[type="submit"]') |
||
54 | .attr('disabled', true) |
||
55 | return true |
||
56 | }) |
||
57 | |||
58 | /** |
||
59 | * Generic confirm form delete using Sweet Alert |
||
60 | */ |
||
61 | $('body') |
||
62 | .on('submit', 'form[name=delete_item]', function(e) { |
||
63 | e.preventDefault() |
||
64 | |||
65 | const form = this |
||
66 | const link = $('a[data-method="delete"]') |
||
67 | const cancel = link.attr('data-trans-button-cancel') |
||
68 | ? link.attr('data-trans-button-cancel') |
||
69 | : 'Cancel' |
||
70 | const confirm = link.attr('data-trans-button-confirm') |
||
71 | ? link.attr('data-trans-button-confirm') |
||
72 | : 'Yes, delete' |
||
73 | const title = link.attr('data-trans-title') |
||
74 | ? link.attr('data-trans-title') |
||
75 | : 'Are you sure you want to delete this item?' |
||
76 | |||
77 | swal({ |
||
78 | title: title, |
||
79 | showCancelButton: true, |
||
80 | confirmButtonText: confirm, |
||
81 | cancelButtonText: cancel, |
||
82 | type: 'warning' |
||
83 | }).then(result => { |
||
84 | result.value && form.submit() |
||
85 | }) |
||
86 | }) |
||
87 | .on('click', 'a[name=confirm_item]', function(e) { |
||
88 | /** |
||
89 | * Generic 'are you sure' confirm box |
||
90 | */ |
||
91 | e.preventDefault() |
||
92 | |||
93 | const link = $(this) |
||
94 | const title = link.attr('data-trans-title') |
||
95 | ? link.attr('data-trans-title') |
||
96 | : 'Are you sure you want to do this?' |
||
97 | const cancel = link.attr('data-trans-button-cancel') |
||
98 | ? link.attr('data-trans-button-cancel') |
||
99 | : 'Cancel' |
||
100 | const confirm = link.attr('data-trans-button-confirm') |
||
101 | ? link.attr('data-trans-button-confirm') |
||
102 | : 'Continue' |
||
103 | |||
104 | swal({ |
||
105 | title: title, |
||
106 | showCancelButton: true, |
||
107 | confirmButtonText: confirm, |
||
108 | cancelButtonText: cancel, |
||
109 | type: 'info' |
||
110 | }).then(result => { |
||
111 | result.value && window.location.assign(link.attr('href')) |
||
112 | }) |
||
113 | }) |
||
114 | }) |
||
115 |